//
//  LCViewController+LCLoadData.m
//  JKFuYinProject
//
//  Created by admin on 2018/6/6.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCViewController+LCLoadData.h"
#import "NetworkTool.h"
#import "NSString+Substring.h"
#import "NetWorkModel.h"
//#define NetworkToolGET [[NetworkTool sharedInstance] POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {\
//if (successBlock) {\
//successBlock(dict);\
//}\
//} failure:^(NSError *error) {\
//if (failureBlock) {\
//failureBlock(error);\
//}\
//}];

#define NetworkToolPOST [[NetworkTool sharedInstance] POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {\
if (successBlock) {\
successBlock(dict);\
}\
} failure:^(NSError *error) {\
if (failureBlock) {\
failureBlock(error);\
}\
}];

@implementation LCViewController (LCLoadData)

- (void)baseNetworkToolPOSTWithUrlString:(NSString *)urlString parameters:(NSMutableDictionary *)parameters successBlock:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSError *))failureBlock {
    [[NetworkTool sharedInstance] POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {
//        NSLog(@"dict = %@", dict);
        NetWorkModel *netWorkModel = [NetWorkModel yy_modelWithJSON:dict];
        if(netWorkModel.status == 1){
            if (successBlock) {
                successBlock(dict);
            }
        }else{
            if (failureBlock) {
                failureBlock(nil);
            }
            [SVProgressHUD showErrorWithStatus:netWorkModel.msg];
        }
    } failure:^(NSError *error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

///add圣经感悟 无图片
- (void)composeUpdateNoImagesWithType:(NSInteger)type shengJingTitle:(NSString *)shengJingTitle notesContentArr:(NSArray *)notesContentArr content:(NSString *)content images:(NSArray *)images success:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSError *))failureBlock {
    NSString *chapter = [shengJingTitle substringWithinBoundsLeft:@"第" right:@"章"];
    NSString *book = [shengJingTitle substringWithinBoundsLeft:@"约" right:@"("];
    NSString *tag = [LCUtils getTagWithNotesContentArr:notesContentArr shengJingTitle:shengJingTitle];
    NSString *urlString = [NSString stringWithFormat:@"%@%@"
                           ,http_zongSJUrl,@"/bible/add"
                           ];
    if (1) {
        NSLog(@"urlString = %@", urlString);
    }
    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @(type), @"type",
                                  book, @"book",
                                  chapter, @"chapter",
                                  tag, @"tag",
                                  content, @"content", nil];
    NSMutableDictionary *parameters
    = [NSMutableDictionary dictionaryWithDictionary:myDictionary];
    if (images.count > 0) {
        NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < images.count; i++) {
            LCUploadImagesModel *uploadImagesModel = images[i];
            [mutableArray addObject:uploadImagesModel.id];
        }
        NSString *string = [mutableArray componentsJoinedByString:@","];//,为分隔符
        [parameters setObject:string forKey:@"images"];
    }
//    NSLog(@"parameters = %@", parameters);
    [self baseNetworkToolPOSTWithUrlString:urlString parameters:parameters successBlock:successBlock failure:failureBlock];
}
///有图片的
- (void)composeUploadTextImagesWithType:(NSInteger)type shengJingTitle:(NSString *)shengJingTitle notesContentArr:(NSArray *)notesContentArr content:(NSString *)content dataImages:(NSArray <UIImage *>*)dataImages success:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSError *))failureBlock  {

    NSString *urlString = [NSString stringWithFormat:@"%@%@"
                           ,http_zongSJUrl,@"/bible/upload"
                           ];
    if (1) {
        NSLog(@"urlString = %@", urlString);
    }
    
    NSMutableDictionary *parameters
    = [NSMutableDictionary dictionaryWithDictionary:@{}];
    
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (NSInteger i = 0; i < dataImages.count; i++) {
        NSData *data = UIImagePNGRepresentation(dataImages[i]);
        [mutableArray addObject:data];
    }
    @weakify(self)
    
    [[NetworkTool sharedInstance] uploadFuYinImagesWithUrlString:urlString input:parameters dataImages:mutableArray.copy filePath:([[NSString stringWithFormat:@"%@",[NSDate.new timeStampStr]] stringByReplacingOccurrencesOfString:@"." withString:@""]) success:^(NSDictionary *dict) {
        @strongify(self)
        
        NetWorkModel *netWorkModel = [NetWorkModel yy_modelWithJSON:dict];
        if (netWorkModel.status == 1) {
            NSArray *array = [NSArray yy_modelArrayWithClass:[LCUploadImagesModel class] json:netWorkModel.data];
            [self composeUpdateNoImagesWithType:type shengJingTitle:shengJingTitle notesContentArr:notesContentArr content:content images:array success:^(NSDictionary *dic) {
                NetWorkModel *netModel = [NetWorkModel yy_modelWithJSON:dict];
                if (netModel.status == 1) {
                    NSLog(@"dic = %@", dic);
                    if (successBlock) {
                        successBlock(dic);
                    }
                }
            } failure:^(NSError *err) {
                if (failureBlock) {
                    failureBlock(err);
                }
            }];
        }
    } failure:^(NSError *error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}
//- (void)composeUploadTextImagesWithType:(NSInteger)type shengJingTitle:(NSString *)shengJingTitle notesContentArr:(NSArray *)notesContentArr content:(NSString *)content dataImages:(NSArray <UIImage *>*)dataImages success:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSError *))failureBlock  {
//    NSString *chapter = [shengJingTitle substringWithinBoundsLeft:@"第" right:@"章"];
//    NSString *book = [shengJingTitle substringWithinBoundsLeft:@"约" right:@"("];
//    NSString *tag = [LCUtils getTagWithNotesContentArr:notesContentArr shengJingTitle:shengJingTitle];
//
//    NSString *urlString = [NSString stringWithFormat:@"%@%@"
//                           ,http_zongSJUrl,@"/bible/add"
//                           ];
//    if (1) {
//        NSLog(@"urlString = %@", urlString);
//    }
//    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
//                                  @(type), @"type",
//                                  book, @"book",
//                                  chapter, @"chapter",
//                                  tag, @"tag",
//                                  content, @"content", nil];
//    NSMutableDictionary *parameters
//    = [NSMutableDictionary dictionaryWithDictionary:myDictionary];
//
//    NSMutableArray *mutableArray = [NSMutableArray array];
//    for (NSInteger i = 0; i < dataImages.count; i++) {
//        NSData *data = UIImagePNGRepresentation(dataImages[i]);
//        [mutableArray addObject:data];
//    }
//
//    [[NetworkTool sharedInstance] uploadFuYinImagesWithUrlString:urlString input:parameters dataImages:mutableArray.copy filePath:([NSString stringWithFormat:@"%@.png",[NSDate.new timeStampStr]]) success:^(NSDictionary *dict) {
//        if (successBlock) {
//            successBlock(dict);
//        }
//    } failure:^(NSError *error) {
//        if (failureBlock) {
//            failureBlock(error);
//        }
//    }];
//}

//- (void)getComposeTextWithType:(NSInteger)type shengJingTitle:(NSString *)shengJingTitle notesContentArr:(NSArray *)notesContentArr success:(void (^)(NSDictionary *))successBlock failure:(void (^)(NSError *))failureBlock {
//    NSString *chapter = [shengJingTitle substringWithinBoundsLeft:@"第" right:@"章"];
//    NSString *book = [shengJingTitle substringWithinBoundsLeft:@"约" right:@"("];
//    NSString *tag = [LCUtils getTagWithNotesContentArr:notesContentArr shengJingTitle:shengJingTitle];
//
//    NSString *urlString = [NSString stringWithFormat:@"%@%@"
//                           ,http_zongSJUrl,@"/bible/lists"
//                           ];
//    if (1) {
//        NSLog(@"urlString = %@", urlString);
//    }
//    //   可选参数    @"fuid": [[NSUserDefaults standardUserDefaults] objectForKey:@"uid"],
//
//    NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
//                                  @(type), @"type",
//                                  book, @"book",
//                                  chapter, @"chapter",
//                                  tag, @"tag",
//                                  @"1", @"page",
//                                  @"30", @"size",
//                                  nil];
//
//    NSMutableDictionary *parameters
//    = [NSMutableDictionary dictionaryWithDictionary:myDictionary];
//
//    [[NetworkTool sharedInstance] POSTFuYinWithUrlString:urlString input:parameters success:^(NSDictionary *dict) {
//        NetWorkModel *netWorkModel = [NetWorkModel yy_modelWithJSON:dict];
//        if(netWorkModel.status == 1){
//            
//            
//            if (successBlock) {
//                successBlock(dict);
//            }
//        }else{
//            if (failureBlock) {
//                failureBlock(nil);
//            }
//            [SVProgressHUD showErrorWithStatus:netWorkModel.msg];
//        }
//    } failure:^(NSError *error) {
//        if (failureBlock) {
//            failureBlock(error);
//        }
//    }];
//}


@end
